﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace GoDartingService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class PlayerDetailService : IPlayerDetailService
    {

        public PlayerDetail AuthenticatePlayer(string userName, string password)
        {
            db3be77145ddab4fc6b01aa0ed010212e6Entities context = new db3be77145ddab4fc6b01aa0ed010212e6Entities();           
            return (from ply in context.PlayerDetails
                    where ply.PlayerUserName == userName && ply.Password == password
                    select ply).FirstOrDefault();
        }


        public void RegisterNewPlayer(PlayerDetail pDetail)
        {
            db3be77145ddab4fc6b01aa0ed010212e6Entities context = new db3be77145ddab4fc6b01aa0ed010212e6Entities();
            context.PlayerDetails.AddObject(pDetail);
            context.SaveChanges();
        }

        public int GetRank(PlayerDetail pDetail)
        {
            int rank = 0;
            db3be77145ddab4fc6b01aa0ed010212e6Entities context = new db3be77145ddab4fc6b01aa0ed010212e6Entities();
            var result = (from ply in context.PlayerDetails
                         orderby ply.HighestScore descending
                         select ply).ToList();
            foreach (var player in result)
            {
                if (player.PlayerUserName == pDetail.PlayerUserName)
                    rank = result.IndexOf(player)+1;
            }

            return rank;                         
        }

        public int GetTotalPlayers()
        {
           db3be77145ddab4fc6b01aa0ed010212e6Entities context = new db3be77145ddab4fc6b01aa0ed010212e6Entities();
           return (from ply in context.PlayerDetails
                         select ply).Count();
        }


        public List<PlayerDetail> GetTop20Players()
        {
            List<PlayerDetail> pList = new List<PlayerDetail>();
            db3be77145ddab4fc6b01aa0ed010212e6Entities context = new db3be77145ddab4fc6b01aa0ed010212e6Entities();
            var result = (from ply in context.PlayerDetails
                          orderby ply.HighestScore descending
                          select ply).ToList();
            int count = result.Count > 20 ? 20 : result.Count;
            for (int i = 0; i < count; i++)
            {
                pList.Add(result[i]);
            }

            return pList;
        }


     

        public bool CheckUsername(string username)
        {
            db3be77145ddab4fc6b01aa0ed010212e6Entities context = new db3be77145ddab4fc6b01aa0ed010212e6Entities();
            bool usernameExists = false;
            foreach (var player in context.PlayerDetails)
            {
                if (player.PlayerUserName == username)
                    usernameExists = true;
            }
            return usernameExists;
        }

        

        
        public void SubmitScore(PlayerDetail player, int score)
        {
            db3be77145ddab4fc6b01aa0ed010212e6Entities context = new db3be77145ddab4fc6b01aa0ed010212e6Entities();
            foreach (var ply in context.PlayerDetails)
            {
                if (ply.PlayerUserName == player.PlayerUserName)
                {
                    if (ply.HighestScore < score)
                        ply.HighestScore = score;
                }
            }
            context.SaveChanges();
            
        }

    }
}
